package cn.yy.cloudpay.util;

import java.util.Locale;

import org.json.JSONException;
import org.json.JSONObject;

import android.content.Context;
import cn.yy.cloudpay.R;
import cn.yy.pos.launcher.service.Operator;

public class ObjectGenerator {
	
	private Context mContext;
	private final int PAID=0;//第三方支付成功
	private final int SCAN_PAY = 1;
	private final int ORDER_QUERY = 2;
	private final int REFUND = 3;
	
	public ObjectGenerator(Context context) {
		this.mContext = context;
	}
	
	private String getString(int resId) {
		return mContext.getString(resId);
	}
	
	/**
	 * 获取微信或支付宝支付的webservice请求参数
	 * @param paytype 支付方式
	 * @param totalFee 金额（以分为单位）
	 * @param authCode 用户授权码
	 * @return
	 * @throws JSONException
	 */
	public String getScanPayReqStr(Integer paytype, int totalFee,Operator operator,String...authCode) throws JSONException {
		String posCode = operator.getPosCode();
		String operator_id = operator.getUniversalid();
		String pos_name = operator.getPosName();
		String unit_id = operator.getGroupID();
		String tenants_name = operator.getTenantsName();
		
		JSONObject json = new JSONObject();
		JSONObject requestParams = new JSONObject();
		JSONObject scanParams = new JSONObject();

		requestParams.put("request_code", SCAN_PAY); // 访问类型

		String out_trade_no = StringUtil.getSeq().toString();
		if(Constant.PAY_TYPE_MICROPAY.equals(paytype)){
			json.put("serviceId", getString(R.string.serviceId_Micropay));
		}else if(Constant.PAY_TYPE_ALIPAY.equals(paytype)){
			json.put("serviceId", getString(R.string.serviceId_Alipay));
		}else if(Constant.PAY_TYPE_ALIPAY_LH.equals(paytype)
				||Constant.PAY_TYPE_MICROPAY_LH.equals(paytype)
				||Constant.PAY_TYPE_HB_LH.equals(paytype)){
			scanParams.put("sendtype",paytype+"");
			json.put("serviceId", getString(R.string.serviceId_lh_pay));
		}else if(Constant.PAY_TYPE_ALIPAY_WF.equals(paytype)
				||Constant.PAY_TYPE_MICROPAY_WF.equals(paytype)){
			scanParams.put("sendtype",paytype+"");
			json.put("serviceId", getString(R.string.serviceId_wf_pay));
		}else if(Constant.PAY_TYPE_YIPAY.equals(paytype)){
			requestParams.put("from","1");
			json.put("serviceId", getString(R.string.serviceId_yipay));
		}else if(Constant.PAY_TYPE_BANKCARD.equals(paytype)){
			json.put("serviceId", getString(R.string.serviceId_BankCard));
		}else if(Constant.PAY_TYPE_FEIXIPAY.equals(paytype)){
			scanParams.put("payType",paytype+"");
			json.put("serviceId", getString(R.string.serviceId_Multi));
		}else{
			scanParams.put("sendtype",paytype+"");
			json.put("serviceId", getString(R.string.mposSaveBillService));
		}

		scanParams.put("total_fee", totalFee);
		scanParams.put("out_trade_no", out_trade_no);
		scanParams.put("auth_code", authCode[0]); // 用户授权码
		if(authCode.length>1) {
			scanParams.put("bankCardType", authCode[1]); // 卡号类型
		}
		if(authCode.length>2) {
			scanParams.put("state", authCode[2]); // 状态
		}
		if(authCode.length>3) {
			scanParams.put("sysretdesc", authCode[3]); // 描述
		}
		if(authCode.length>4) {
			scanParams.put("out_trade_no", authCode[4]); // 订单号
		}
		scanParams.put("device_info", posCode); // 设备码
		scanParams.put("operator_id", operator_id); // 操作员ID
		scanParams.put("operator_account", operator.getOperatorAccount()); // 操作员编号
		scanParams.put("pos_name", pos_name); // POS机名称
		scanParams.put("unit_id", unit_id); // 商户ID
		scanParams.put("tenants_no", operator.getTenantsID()); // 门店编号
		scanParams.put("tenants_name", tenants_name); // 门店名称

		requestParams.put("scan_params", scanParams);
		String para_str = requestParams.toString();
		String recordKey = MD5.get32MD5Str(para_str + "dtf_webserviceRequset",
				"UTF-8").toUpperCase(Locale.getDefault());
		json.put("parameters", requestParams);
		json.put("recordKey", recordKey);
		return json.toString();
	}
	
	public String getScanRetundReqStr(Integer paytype, int totalFee,Operator operator,String...authCode) throws JSONException {
		String posCode = operator.getPosCode();
		String operator_id = operator.getUniversalid();
		String pos_name = operator.getPosName();
		String unit_id = operator.getGroupID();
		String tenants_name = operator.getTenantsName();
		
		JSONObject json = new JSONObject();
		JSONObject scanParams = new JSONObject();

		JSONObject requestParams = new JSONObject();
		String out_trade_no = StringUtil.getSeq().toString();
		if(Constant.PAY_TYPE_MICROPAY.equals(paytype)){
			json.put("serviceId", getString(R.string.serviceId_Micropay));
		}else if(Constant.PAY_TYPE_ALIPAY.equals(paytype)){
			json.put("serviceId", getString(R.string.serviceId_Alipay));
		}else if(Constant.PAY_TYPE_BANKCARD.equals(paytype)){
			json.put("serviceId", getString(R.string.serviceId_BankCard));
		}else if(Constant.PAY_TYPE_ALIPAY_LH.equals(paytype)
				||Constant.PAY_TYPE_MICROPAY_LH.equals(paytype)
				||Constant.PAY_TYPE_HB_LH.equals(paytype)){
			scanParams.put("sendtype",paytype+"");
			json.put("serviceId", getString(R.string.serviceId_lh_pay));
		}else if(Constant.PAY_TYPE_ALIPAY_WF.equals(paytype)
				||Constant.PAY_TYPE_MICROPAY_WF.equals(paytype)){
			scanParams.put("sendtype",paytype+"");
			json.put("serviceId", getString(R.string.serviceId_wf_pay));
		}else{
			scanParams.put("sendtype",paytype+"");
			json.put("serviceId", getString(R.string.mposSaveBillService));
		}

		scanParams.put("total_fee", totalFee);
		scanParams.put("auth_code", authCode[0]); // 用户授权码
		if(authCode.length>1) {
			scanParams.put("bankCardType", authCode[1]); // 卡号类型
		}
		if(authCode.length>2) {
			scanParams.put("state", authCode[2]); // 状态
		}
		if(authCode.length>3) {
			scanParams.put("sysretdesc", authCode[3]); // 描述
		}
		scanParams.put("device_info", posCode); // 设备码
		scanParams.put("operator_id", operator_id); // 操作员ID
		scanParams.put("pos_name", pos_name); // POS机名称
		scanParams.put("out_trade_no", out_trade_no);
		scanParams.put("unit_id", unit_id); // 商户ID
		scanParams.put("tenants_name", tenants_name); // 门店名称
		requestParams.put("request_code", REFUND); // 访问类型（退款）
		requestParams.put("scan_params", scanParams);
		String para_str = requestParams.toString();
		String recordKey = MD5.get32MD5Str(para_str + "dtf_webserviceRequset",
				"UTF-8").toUpperCase(Locale.getDefault());
		json.put("parameters", requestParams);
		json.put("recordKey", recordKey);
		return json.toString();
	}

}
